<template>
  <div class="header-one">
    <div class="header-two">
      <div class="header-three">
        <span>欢迎来到读者书店!</span>
        <div>
          <span v-if="this.name !== null"
            >{{ this.name }}&nbsp;&nbsp;<span
              @click="tuichu"
              style="cursor: pointer"
              >退出</span>
            </span>
          <span v-if="this.name==null"
            ><router-link to="/Login"><span>登录</span></router-link></span
          >
          <span>|</span>
          <router-link to="/Register"><span>注册</span></router-link>
        </div>
      </div>
    </div>
    <div class="header-four">
      <router-link to="/">
        <img src="/img/logo.jpeg" alt="" style="width: 80px"
      /></router-link>
      <div class="header-five">
        <input v-model="input" type="text" @keyup.enter="searchone" />
        <img
          style="width: 40px"
          src="../assets/shousuo.png"
          alt=""
          @click="serach"
        />
      </div>
      <div>
      </div>
    </div>
    <div class="header-ten">
      <div class="eleven">
        <router-link to="/"><span>图书首页</span> </router-link>
        <router-link to="/new"><span>最新上架</span></router-link>
        <router-link to="/ranking"><span>图书排行</span></router-link>
        <router-link to="/special"><span>图书专题</span></router-link>
        <router-link to="/shopping"><span @click="gouwu">购物车<i class="el-icon-shopping-cart-1"></i></span></router-link>
      </div>
    </div>
  </div>
</template>
<script>
import { mapState, mapMutations } from "vuex";
export default {
  name: "myHeader",
  data() {
    return {
      input: "",
    };
  },
  methods: {
    ...mapMutations(["setname"]),
    searchone() {
      this.axios.get(`booksearch?title=${this.input}`).then((result) => {
        console.log(result.data);
        if (result.data.length == 0) {
          alert("暂无此商品,请等待后续更新");
        } else {
          this.$router.push(`/detail/${result.data[0].id}`);
          setTimeout(function () {
            location.reload(true);
          }, 100);
        }
      });
    },
    serach() {
      this.axios.get(`booksearch?title=${this.input}`).then((result) => {
        console.log(result.data);
        if (result.data.length == 0) {
          alert("暂无此商品,请等待后续更新");
        } else {
          this.$router.push(`/detail/${result.data[0].id}`);
          setTimeout(function () {
            location.reload(true);
          }, 100);
        }
      });
    },
    tuichu() {
      sessionStorage.removeItem("user")
      location.reload(true)
    },
    gouwu(){
      if(this.name==null){
        this.$alert('你暂未登录请前往登录','提示')
      }else{
        this.$router.push('/shopping')
      }
    }
  },
  computed: {
    ...mapState(["name"]),
  },
};
</script>
<style scoped>
p,
span {
  font-family: "微软雅黑";
}
.header-one {
  min-width: 1200px;
  /* border-bottom: 1px solid black; */
}
.header-two {
  border: 1px solid rgb(205, 205, 205);
  background-color: #f7f7f7;
  /* height: 30px; */
}
.header-three {
  width: 1200px;
  margin: 5px auto;
  display: flex;
  justify-content: space-between;
}
.header-three span {
  margin-right: 10px;
  color: rgb(0, 0, 0);
  font-weight: bold;
}
.header-four {
  width: 1200px;
  margin: 10px auto;
  display: flex;
  justify-content: space-between;
}
.header-five input {
  height: 33px;
  width: 350px;
  border: 2px solid #ccc;
  background-repeat: no-repeat;
  background-size: 36px;
  background-position: right;
  border-radius: 6px;
}
.header-five {
  margin: auto 0;
}
.header-six {
  background-color: #f9f9f9;
  border: 1px solid #dfdfdf;
  /* border-bottom:none ; */
  width: 135px;
  height: 36px;
  cursor:pointer;
  margin: auto 0;
  line-height: 36px;
  background-size: 20px;
  background-position: left;
  background-repeat: no-repeat;
  background-image: url(../assets/gouwuche.webp);
}
.header-six > a {
  margin-left: 40px;
  color: #666;
  line-height: 35px;
}
.header-seven {
  width: 350px;
  height: 200px;
  background-color: #f9f9f9;
  border: 1px solid #dfdfdf;
  display: none;
  float: right;
  position: relative;
  z-index: 5;
}
.header-six:hover .header-seven {
  display: block;
  box-shadow: 0px 8px 10px gray;
}
.header-seven > p {
  color: #666;
  line-height: 175px;
  text-align: center;
}
.header-eight {
  width: 120px;
  height: 36px;
  margin: auto 0;
  background-color: #f9f9f9;
  border: 1px solid #dfdfdf;
}
.header-eight > p {
  margin-left: 20px;
  color: black;
  line-height: 35px;
}
.header-nine {
  width: 120px;
  height: 120px;
  background-color: #f9f9f9;
  border: 1px solid #dfdfdf;
  float: left;
  display: none;
  position: relative;
  z-index: 5;
}
.header-eight:hover p {
  color: #666;
  cursor: pointer;
}
.header-eight:hover .header-nine {
  display: block;
}
.header-ten {
  width: 1200px;
  margin: 20px auto;
  background-color: #fcf8f8;
}
.eleven {
  line-height: 50px;
  display: flex;
  justify-content: space-around;
}
.eleven span {
  padding: 15px 50px;
  color: black;
}
.eleven span:hover {
  background-color: #ccc;
}
.eleven a:focus {
  background-color: #ccc;
}
</style>

